package org.bigjava.controller;

import org.bigjava.bean.Wuye;
import org.bigjava.bean.Xiaoqu;
import org.bigjava.biz.WuyeService;
import org.bigjava.biz.XiaoquService;
import org.bigjava.common.Constant;
import org.bigjava.common.FenYe;
import org.bigjava.common.ShangChuan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class XiaoquController {

    @Autowired
    HttpSession session;

    @Autowired
    XiaoquService xiaoquService;
    @Autowired
    WuyeService wuyeService;
    //修改小区
    @RequestMapping("xiaoqu/xiugai")
    public String xiaoQuXiuGai(MultipartFile file, Xiaoqu xiaoqu, HttpServletRequest request){
        System.out.println("修改小区的内容"+xiaoqu);
        Xiaoqu xiaoqua = (Xiaoqu) session.getAttribute("xiaoqu");
        xiaoqu.setId(xiaoqua.getId());
        if(file.isEmpty()){
            xiaoqu.setImgurl(xiaoqua.getImgurl());
        }else{
            String imgurl = ShangChuan.saveFile(request,file);
            xiaoqu.setImgurl(imgurl);
        }
        System.out.println(xiaoqu.getImgurl());
        int i = xiaoquService.xiaoQuXiuGai(xiaoqu);
        return "redirect:/xiaoqu/fenye";
    }
    //添加小区
    @PostMapping("xiaoqu/tianjia")
    public String xiaoQuTianJia(@RequestParam("file")MultipartFile file, Xiaoqu xiaoqu, HttpServletRequest request) throws IOException {
        String imgurl = ShangChuan.saveFile(request,file);
        xiaoqu.setImgurl(imgurl);
        int i = xiaoquService.xiaoQuAdd(xiaoqu);
        return "redirect:/xiaoqu/fenye";
    }
    //搜索小区
    @RequestMapping("xiaoqu/sousuo")
    public String xiaoQuSouSuo(String dyg,String deg){
        List<Xiaoqu> lists = xiaoquService.xiaoQuMoHu(dyg,deg);
        session.setAttribute("xiaoqus",lists);
        return "xqgl";
    }
    //删除小区
    @RequestMapping("xiaoqu/shanchu")
    public String xiaoQuShanChu(int id){
        System.out.println("要删除的小区id:"+id);
        xiaoquService.xiaoQuShanChu(id);
        Object a = session.getAttribute("yeshu");
        return "redirect:/xiaoqu/fenye?a="+a;
    }
    //跳转到小区修改页面(动态拿到物业列表)
    @RequestMapping("xiaoqu/xg")
    public String xqxg(int id){
        Xiaoqu xiaoqu = xiaoquService.xiaoQuId(id);
        session.setAttribute("xiaoqu",xiaoqu);
        List<Wuye> wuyes = wuyeService.wuYeQuan();
        session.setAttribute("wuyes",wuyes);
        return "xqxg";
    }
    //跳转到小区添加页面(动态拿到物业列表)
    @RequestMapping("xiaoqu/xqtj")
    public String xqtj(){
        List<Wuye> wuyes = wuyeService.wuYeQuan();
        session.setAttribute("wuyes",wuyes);
        return "xqtj";
    }

    //小区分页查询
    @RequestMapping("xiaoqu/fenye")
    public String xiaoQuFenYe(String a){
        //业主总人数
        int zong =  xiaoquService.xiaoQuZong();
        session.setAttribute("zong",zong);
        //调用分页方法返回下一页开始id
        int ks = FenYe.fenYe(session,zong,a, Constant.fenye);
        //分页查询出的业主
        List<Xiaoqu> lists = xiaoquService.xiaoQuFenYe(ks,Constant.fenye);
        session.setAttribute("xiaoqus",lists);
        return "xqgl";
    }

    //查出全部小区
    @RequestMapping("xiaoqu/quan")
    public String xiaoQuQuan(){
        List<Xiaoqu> lists = xiaoquService.xiaoQuQuan();
        session.setAttribute("xiaoqus",lists);
        return "yztj.html";
    }
}
